var jsonFile = require('jsonfile')
var fileName = 'xinghan.json'
const axios = require('axios')
var jsonData = jsonFile.readFileSync(fileName)
const qs = require('qs')

const phone = jsonData.phone
const pwd = jsonData.pwd
const token = jsonData.token
const Xtoken = jsonData.Xtoken
const pay_pwd = jsonData.pay_pwd
const goods = jsonData.goods
var requestArray = []

const instance = axios.create({
  credentials: true,
  baseURL: 'https://www.xhan.art',
  // headers: { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36' }
})
instance.defaults.headers['Content-Type'] = 'application/json'
instance.defaults.headers['Authorization'] = token

const Xinstance = axios.create({
  credentials: true,
  baseURL: 'https://www.xhan.art',
  // headers: { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36' }
})
Xinstance.defaults.headers['Content-Type'] = 'application/json'
Xinstance.defaults.headers['Authorization'] = Xtoken






pushList()


// 商品列表填充
function pushList() {
  setInterval(() => {
    for (let item in goods) {
      Xinstance.post('/api/api/collection-sell/queryCollectionByConId', {
        amount: 0,
        blindBoxId: "",
        collectionId: goods[item].id,
        finished: true,
        loading: true,
        number: null,
        pageNum: 1,
        pageSize: 10,
        total: 0,
        totalPages: 1
      }).then(res => {
        let array = res.data.data.records
        console.log(array.length)
        array.forEach(data => {
          let price = data.amount
          let id = data.userCollectionSellId
          let name = data.collection.name
          let status = data.payorderStatus
          console.log('商品:' + name + '最低价为:' + price + '------价格低于:' + goods[item].price + '时将自动锁定' + status)
          if (price <= goods[item].price && status != 1) {
            instance.post('/api/api/collectionOrder/shopping', { number: 1, useSellId: id }).then(res => {
              console.log(res.data)
              if (res.data.success) {
                axios.post('https://open.feishu.cn/open-apis/bot/v2/hook/9234b14f-e535-41e2-98dd-309f1fd2b580', { "msg_type": "text", "content": { "text": "星汉锁单成功,请上号支付!!!" + ",价格为:" + price } })
                let orderCode = res.data.data
                instance.post('/api/api/collectionOrder/shoppingPay2?orderCode=' + orderCode + '&payPassword=&payType=4').then(res => {
                  if (res.data.success) {
                    let url = res.data.data.message
                    axios.post('https://open.feishu.cn/open-apis/bot/v2/hook/9234b14f-e535-41e2-98dd-309f1fd2b580', { "msg_type": "text", "content": { "text": url } })
                  }
                }).catch(err => {
                  console.log('提取链接失败')
                })
              } else {}
            }).catch(err => {
              console.log('购买失败')
            })
          }
        });
      }).catch(err => {
        console.log('请求频繁')
      })
    }
  }, 10)
}